<?php

//if (!function_exists('get_profile_detail')) {

    function get_profile_detail($str_id='',$id=''){

        $data_output['success'] = "false";
        $userid = $str_id;
        $myid = $id;
        $myStatus = get_instance()->input->post('myStatus');
        $query_user = get_instance()->db->query("SELECT username, name,password,isPremium,entryDate,lastUpdated,avatar,lastUpdatedAvatar,gender, (SELECT SUM(rateValue)/COUNT(rateValue) FROM rating WHERE providerId = ".$userid." AND rateValue >0) AS rate, (SELECT COUNT(rateValue) FROM rating WHERE providerId = ".$userid." AND rateValue >0) AS voterCount FROM `user` WHERE `id` = '".$userid."'");
        $row_user = $query_user->row();
        if($query_user->num_rows() > 0){
            $data_output['success'] = "true";
            $data_output['id'] = $userid;
            $data_output['username'] = $row_user->username;
            $data_output['password'] = $row_user->password;
            $data_output['name'] = $row_user->name;
            $data_output['isPremium'] = $row_user->isPremium;
            $data_output['entryDate'] = $row_user->entryDate;
            $data_output['lastUpdated'] = $row_user->lastUpdated;
            $data_output['avatar'] = $row_user->avatar;
            $data_output['lastUpdatedAvatar'] = $row_user->lastUpdatedAvatar;
            $data_output['gender'] = $row_user->gender;
            $data_output['rate'] = "".$row_user->rate;
            $data_output['voterCount'] = "".$row_user->voterCount;

            $query_userprofile = get_instance()->db->query("SELECT phoneNumber,bio,address,avatarMotor,lastUpdatedAvatarMotor,motorDesc,motorYear,licenseNumber,deliverCounter,email FROM `userprofile` WHERE `idUser` = '".$userid."'");
            $row_userprofile = $query_userprofile->row();
            $data_output['phoneNumber'] = $row_userprofile->phoneNumber;
            $data_output['bio'] = $row_userprofile->bio;
            $data_output['address'] = $row_userprofile->address;
            $data_output['avatarMotor'] = $row_userprofile->avatarMotor;
            $data_output['lastUpdatedAvatarMotor'] = $row_userprofile->lastUpdatedAvatarMotor;
            $data_output['licenseNumber'] = $row_userprofile->licenseNumber;
            $data_output['motorDesc'] = $row_userprofile->motorDesc;
            $data_output['motorYear'] = $row_userprofile->motorYear;
            $data_output['deliverCounter'] = $row_userprofile->deliverCounter;
            $data_output['email'] = $row_userprofile->email;
        }
        
        $query = get_instance()->db->query("SELECT t.* FROM trip t JOIN `user` u ON u.id=t.userId WHERE departTime >= '".date("Y-m-d H:i:s")."' AND userId = ".$userid." AND t.is_available=1 ORDER BY departTime LIMIT 1");
        if($query->num_rows() < 1){
            $data_output['currentTrip'] = "";
        }else{
            $row = $query->row(); 
            $data_output["currentTrip"][0]["id"] = $row->id;
            $data_output["currentTrip"][0]["status"] = $row->status;
            $data_output["currentTrip"][0]["source_label"] = $row->source_label;
            $data_output["currentTrip"][0]["source_latitude"] = $row->source_latitude;
            $data_output["currentTrip"][0]["source_longitude"] = $row->source_longitude;
            $data_output["currentTrip"][0]["destination_label"] = $row->destination_label;
            $data_output["currentTrip"][0]["destination_latitude"] = $row->destination_latitude;
            $data_output["currentTrip"][0]["destination_longitude"] = $row->destination_longitude;
            $data_output["currentTrip"][0]["via"] = $row->via;
            $data_output["currentTrip"][0]["priceInRupiah"] = $row->priceInRupiah;
            $data_output["currentTrip"][0]["priceInPoint"] = $row->priceInPoint;
            $data_output["currentTrip"][0]["departTime"] = $row->departTime;
            $data_output["currentTrip"][0]["notes"] = $row->notes;
            if($row->status == 'P' && $myid != $userid)             $data_output["pickUpStatus"] = get_instance()->getPickupStatus($myid,$userid);
        }
        
        
        $privacy_criteria = " AND t.privacy = 0";
        if($myid === $userid)           $privacy_criteria = "";

        $query = get_instance()->db->query("SELECT COUNT(*) AS historyCount FROM `transaction` t JOIN transactionStatus s ON t.id=s.idTransaction WHERE s.`status` = 'F' AND (userId = ".$userid." OR providerId = ".$userid.")".$privacy_criteria.' ORDER BY t.id DESC LIMIT 3');
        $row = $query->row();
        $historyCount = $row->historyCount;
        $data_output["viewMoreHistory"] = "false";
        $data_output['history'] = "";
        if($historyCount > 0){
            if($historyCount > 3){
                $data_output["viewMoreHistory"] = "true";
            }
            $query = get_instance()->db->query("SELECT DISTINCT t.* FROM `transaction` t JOIN transactionStatus s ON t.id=s.idTransaction WHERE s.`status` = 'F' AND (userId = ".$userid." OR providerId = ".$userid.")".$privacy_criteria.' ORDER BY t.id DESC LIMIT 3');
            $row = $query->row();
            if($query->num_rows() < 1 || $historyCount < 1){
                $data_output['history'] = "";
            }else{
                $count = 0;
                foreach($query->result() as $row){
                    $data_output["history"][$count]["id"] = $row->id;
                    $data_output["history"][$count]["departTime"] = $row->departTime;
                    $data_output["history"][$count]["userId"] = $row->userId;
                    $data_output["history"][$count]["name"] = get_instance()->getNameById($row->userId);
                    $data_output["history"][$count]["avatar"] = get_instance()->getAvatarById($row->userId);
                    $data_output["history"][$count]["lastUpdatedAvatar"] = get_instance()->getLastUpdatedAvatar($row->userId);
                    $data_output["history"][$count]["providerId"] = $row->providerId;
                    $data_output["history"][$count]["providerName"] = get_instance()->getNameById($row->providerId);
                    $data_output["history"][$count]["providerAvatar"] = get_instance()->getAvatarById($row->providerId);
                    $data_output["history"][$count]["providerLastUpdatedAvatar"] = get_instance()->getLastUpdatedAvatar($row->providerId);
                    $data_output["history"][$count]["source_label"] = $row->source_label;
                    $data_output["history"][$count]["source_latitude"] = $row->source_latitude;
                    $data_output["history"][$count]["source_longitude"] = $row->source_longitude;
                    $data_output["history"][$count]["destination_label"] = $row->destination_label;
                    $data_output["history"][$count]["destination_latitude"] = $row->destination_latitude;
                    $data_output["history"][$count]["destination_longitude"] = $row->destination_longitude;
                    $data_output["history"][$count]["paymentMethod"] = $row->paymentMethod;
                    $data_output["history"][$count]["price"] = $row->price;
                    $idTrip = $row->idTrip;
                    $query = get_instance()->db->query("select via,priceInRupiah,priceInPoint from trip where id =".$idTrip);
                    $row = $query->row();
                    $data_output["history"][$count]["via"] = $row->via;
                    $count++;
                }
                if($count == 0)             $data_output['history'] = "";
            }
        }
        
        $providerid = get_instance()->input->post('providerId');
        
        $query = get_instance()->db->query("SELECT COUNT(*) AS voterCount FROM rating WHERE providerId = ".$userid);
        $row = $query->row();
        $voterCount = $row->voterCount;
        $data_output['viewMoreComments'] = "false";
        $data_output['comments'] = "";
        if($voterCount > 0){
            if($voterCount > 3){
                $data_output['viewMoreComments'] = "true";
            }
            $count = 0;
            $query = get_instance()->db->query("SELECT reviewerId, u.`name`, u.avatar, u.lastUpdatedAvatar, rateValue, `comment` FROM rating r JOIN `user` u ON u.id=r.reviewerId WHERE providerId = ".$userid." AND rateValue > 0 AND `comment` IS NOT NULL AND `comment` <> '' LIMIT 3");
            $row = $query->row();
            if($query->num_rows() < 1 || $voterCount < 1){
                $data_output['comments'] = "";
            }else{
                foreach($query->result() as $row){
                    $data_output['comments'][$count]['reviewerId'] = $row->reviewerId;
                    $data_output['comments'][$count]['name'] = $row->name;
                    $data_output['comments'][$count]['avatar'] = $row->avatar;
                    $data_output['comments'][$count]['lastUpdatedAvatar'] = $row->lastUpdatedAvatar;
                    /*$data_output['rating'][$count]['rateValue'] = ''.$row->rateValue;*/
                    $data_output['comments'][$count]['comment'] = $row->comment;
                    $count++;
                }
                if($count == 0)             $data_output['comments'] = "";
            }
        }
        
        $query = get_instance()->db->query("SELECT `comment`, rateValue FROM rating  WHERE providerId = ".$userid." AND reviewerId = ".$myid." LIMIT 1");
        $row = $query->row();
        if($query->num_rows() < 1){
            $data_output['myRating'] = "";
        }else{
            $data_output['myRating'][0]['rateValue'] = ''.$row->rateValue;
            $data_output['myRating'][0]['comment'] = $row->comment;
        }
        return $data_output;
     }

//}
?>
